import requests
import mysql.connector

def pa():
    #学校名称
    schoolName = "武汉大学"

    #爬取链接
    url = "http://xsjy.whu.edu.cn/jyxt/sczp/zpztgl/getZpztdwList.zf?send_by_bootstrap_table=true&searchModel.inputType=%7B%22zpzt%22%3A%5B%22%22%5D%2C%22zwmc%22%3A%5B%22%22%5D%2C%22dwmc%22%3A%5B%22%22%5D%7D&searchModel.selectType=%7B%7D&searchModel.dateType=%7B%7D&searchModel.numberType=%7B%7D&searchModel.inputSqlType=0&queryModel.showCount=1000000&queryModel.currentPage=1"

    #请求头
    headers = {

    }
    #爬取数据并解析
    html = requests.get(url=url, headers=headers)

    #解析JSON
    dataList = html.json().get("items")

    for data in dataList:

        print(data.get("zpzt")+"|"+data.get("fbsj"))
        mysql_conn(data.get("zpzt"),schoolName,data.get("fbsj"))


#数据库连接
def mysql_conn(job_title,school_name,create_time):
    mydb = mysql.connector.connect(
        host="127.0.0.1",
        user="root",
        passwd="",
        database="hschool_job"
    )
    mycursor = mydb.cursor()

    #插入
    sql = "INSERT INTO h_job (job_title,school_name,create_time) VALUES (%s,%s,%s)"
    val = (job_title,school_name,create_time)
    mycursor.execute(sql, val)
    mydb.commit()

if __name__ == '__main__':
    pa()
